package biz.appframe;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.mysema.query.BooleanBuilder;
import com.rfid.project.exception.CannotDelException;

@Service
@Transactional
public class ModuleService {

	@Autowired
	private ModuleDao moduleDao;

	private QModule $ = QModule.module;

	@Autowired
	private MenuDao menuDao;
	
	private QMenu $_ = QMenu.menu;

	@Autowired
	public void setModuleDao(ModuleDao moduleDao) {
		this.moduleDao = moduleDao;
	}

	@Autowired
	public void setMenuDao(MenuDao menuDao) {
		this.menuDao = menuDao;
	}

	public List<Module> findAll() {
		Sort sortObject = new Sort(Direction.ASC, "orderNumber");
		return this.moduleDao.findAll(sortObject);
	}
	
	public List<Module> findModuleByMenus(List<Menu> menus){
		Set<String> names = new HashSet<String>();
		for (Menu menu : menus) {
			names.add(menu.getModuleCode());
		}
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($.moduleCode.in(names));
		Sort sortObject = new Sort(Direction.ASC, "orderNumber");
		PageRequest pageRequest = new PageRequest(0, Integer.MAX_VALUE, sortObject);
		return this.moduleDao.findAll(condition, pageRequest).getContent();
	}
	
	public void save(Module module) {
		module.setOrderNumber(maxorder() + 1);
		this.moduleDao.save(module);
	}
	private int maxorder(){
		BooleanBuilder condition = new BooleanBuilder();
		//OrderSpecifier<Integer> order = $.orderNumber.desc();
		//moduleDao.findAll(condition, order);
		Sort sortObject = new Sort(Direction.DESC, "orderNumber");
		PageRequest pageRequest = new PageRequest(0, 1, sortObject);
		Page<Module> modules = this.moduleDao.findAll(condition,pageRequest);
		if(modules.getTotalElements() > 0){
			Module module = modules.getContent().get(0);
			return module.getOrderNumber() + 1;
		}
		return 1;
	}
	
	public void delete(Long id) throws CannotDelException{
		if (this.findMenuByModule(this.moduleDao.findOne(id).getModuleCode())) {
			throw new CannotDelException("模块已经被菜单引用，请先删除菜单或修改菜单的引用！");
		} else {
			this.moduleDao.delete(id);
		}
	}
	
	private boolean findMenuByModule(String moduleCode){
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($_.moduleCode.eq(moduleCode));
		return this.menuDao.count(condition) > 0;
	}
	
	public boolean changeOrderNumber(Long id, Long targetId){
		Module currentModule = this.moduleDao.findOne(id);
		Module targetModule = this.moduleDao.findOne(targetId);
		int orderNumber = currentModule.getOrderNumber();
		currentModule.setOrderNumber(targetModule.getOrderNumber());
		targetModule.setOrderNumber(orderNumber);
		
		this.moduleDao.save(currentModule);
		this.moduleDao.save(targetModule);
		return true;
	}

	public Module findOne(Long id) {
		return this.moduleDao.findOne(id);
	}
}
